/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.rpc.service.plugins  
 */
package com.apache.rpc.service.plugins;

import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.service.impl.helper.CacheTactics;

/**
 * description:  按BeanId获取接口信息
 * @author Hou Dayu 创建时间：2015-4-2  
 * @Copyright(c)2014:
 */
public class InterfaceRegisterInfoPlugin implements PluginConnector {

	private CacheTactics interfaceRegisterCacheTactics;

	/**
	 * TODO 按BeanId获取接口信息.  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity result = new ResultEntity();
		String beanId = String.valueOf(param.getParams("beanId"));
		String methodKey = String.valueOf(param.getParams("methodKey"));
		if (Validator.isNull(beanId) || Validator.isNull(methodKey)) {
			result.setResult("false");
			result.setMessage("方法参数不可为空值");
			return result;
		}
		Map<String, List<InterfaceRegister>> list = interfaceRegisterCacheTactics.selectInterfaceRegisterCache(beanId,
				"1", methodKey);
		InterfaceRegister register = getServiceKey(list);
		if (!Validator.isEmpty(register)) {
			result.setEntity(register);
			result.setResult("true");
			result.setMessage("接口调用成功");
		} else {
			result.setResult("false");
			result.setMessage("没有配置相关接口注册信息");
		}
		return result;
	}

	private InterfaceRegister getServiceKey(Map<String, List<InterfaceRegister>> list) {
		if (Validator.isEmpty(list))
			return null;
		InterfaceRegister register = null;
		if (!Validator.isEmpty(list.get("internal"))) {
			register = list.get("internal").get(0);
		} else if (!Validator.isEmpty(list.get("socket"))) {
			register = list.get("socket").get(0);
		} else if (!Validator.isEmpty(list.get("webservice"))) {
			register = list.get("webservice").get(0);
		} else if (!Validator.isEmpty(list.get("https"))) {
			register = list.get("https").get(0);
		} else if (!Validator.isEmpty(list.get("http"))) {
			register = list.get("http").get(0);
		}
		return register;
	}

	public void setInterfaceRegisterCacheTactics(CacheTactics interfaceRegisterCacheTactics) {
		this.interfaceRegisterCacheTactics = interfaceRegisterCacheTactics;
	}
}
